package com.homson.servicedomain.domain.user;

import com.homson.coreservice.servicecommon.expection.SystemException;
import com.homson.coreservice.servicecommon.co.UserCreateCO;
import com.homson.coreservice.servicecommon.utils.IDWorkerUtils;
import com.homson.coreservice.serviceinfrastructure.datasource.model.user.UserDO;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author: homson_leo
 * @contact: hey_超级巨星（Homson_Leo）- 刘泓岑 1196816884@qq.com
 * @time: 2025/10/4 11:30 AM
 **/
@Component
public class UserCreateCommand extends BaseCommand{
    public void registerOrUpdate(Long userId, UserCreateCO userCreateCmd) throws SystemException {
        try{
            if(Objects.isNull(userId)){
                // TODO 做密等 加锁
                // 雪花算法生成
                userCreateCmd.setUserId(IDWorkerUtils.generateSnowId());
            }
            UserDO userDO = parseUser(userCreateCmd);
            userRepository.createOrUpdate(userDO);
        }catch (Exception e){
            throw new SystemException(e.getMessage());
        }
        
    }
    
    private UserDO parseUser(UserCreateCO userCreateCmd) {
        UserDO userDO = new UserDO();
        userDO.setUserId(userCreateCmd.getUserId());
        userDO.setName(userCreateCmd.getUserName());
        userDO.setEmail(userCreateCmd.getEmail());
        return userDO;
    }
    
}
